package com.manli.api.controller;


import com.manli.api.base.annotation.Permissions;
import com.manli.api.dto.*;
import com.manli.api.enums.AuthEnums;
import com.manli.api.pojo.Result;
import com.manli.api.pojo.SessionInfo;
import com.manli.api.pojo.UserPermission;
import com.manli.api.service.UserPermissionService;
import com.manli.api.util.JwtUtils;
import com.manli.api.util.ResultUtil;
import com.manli.api.util.SessionInfoUtils;

import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
public class UserPermissionController {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	
	@Autowired
	UserPermissionService userPermissionService;
	@Autowired
	SessionInfoUtils sessionInfoUtils;
	/**
	 * 获取用户权限
	 * @return
	 */
	@RequestMapping(value="/userPermission/getUserPermission")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN_PERMISSONS)
	public Result getUserPermission(HttpServletRequest request,HttpServletResponse response,@RequestBody Map map) {
		return ResultUtil.success(userPermissionService.getUserPermission(Integer.parseInt(map.get("userId").toString()),request));
	}

	/**
	 * 获取用户权限,符合vue-router格式
	 * @return
	 */
	@RequestMapping(value="/userPermission/getUserPermissionVue")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result getUserPermissionVue(HttpServletRequest request) {
		String authToken = request.getHeader(AuthEnums.Auth_TOKEN.getContent());
		Claims claims = JwtUtils.verifyJwt(authToken);
		SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        return ResultUtil.success(userPermissionService.getUserPermissionVue((int)claims.get("userId"),sessionInfo.getBranchId()));
	}

	/**
	 * 添加用户
	 */
	@RequestMapping(value="/userPermission/saveUser")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result saveUser(HttpServletRequest request,HttpServletResponse response,@RequestBody User user) {
		userPermissionService.saveUser(user);
		return ResultUtil.success("用户保存成功");
	}
	/**
	 * 更新用户
	 */
	@RequestMapping(value="/userPermission/updateUser")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result updateUser(HttpServletRequest request,HttpServletResponse response,@RequestBody User user) {
		userPermissionService.updateUser(user);
		return ResultUtil.success("用户修改成功");
	}
	/**
	 * 添加角色
	 */
	@RequestMapping(value="/userPermission/saveRole")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result saveRole(HttpServletRequest request,HttpServletResponse response,@RequestBody Role role) {
		userPermissionService.saveRole(role);
		return ResultUtil.success("角色保存成功");
	}
	/**
	 * 更新角色
	 */
	@RequestMapping(value="/userPermission/updateRole")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result updateRole(HttpServletRequest request,HttpServletResponse response,@RequestBody Role role) {
		userPermissionService.updateRole(role);
		return ResultUtil.success("角色修改成功");
	}
	/**
	 * 添加权限
	 */
	@RequestMapping(value="/userPermission/savePermission")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result savePermission(HttpServletRequest request,HttpServletResponse response,@RequestBody Permission permission) {
		userPermissionService.savePermission(permission);
		return ResultUtil.success("权限保存成功");
	}
	/**
	 * 更新权限 
	 */
	@RequestMapping(value="/userPermission/updatePermission")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result updatePermission(HttpServletRequest request,HttpServletResponse response,@RequestBody Permission permission) {
		userPermissionService.updatePermission(permission);
		return ResultUtil.success("权限修改成功");
	}
	/**
	 * 添加  角色 +权限
	 */
	@RequestMapping(value="/userPermission/saveRolePermission")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result saveRolePermission(HttpServletRequest request,HttpServletResponse response,@RequestBody List<RolePermission> rolePermissions) {
		userPermissionService.saveRolePermission(rolePermissions);
		return ResultUtil.success("角色权限添加成功");
	}
	/**
	 * 添加用户+角色
	 */
	@RequestMapping(value="/userPermission/saveUserRole")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result saveUserRole(HttpServletRequest request,HttpServletResponse response,@RequestBody List<UserRole> userRoles) {
		userPermissionService.saveUserRoles(userRoles);
		return ResultUtil.success("用户角色添加成功");
	}
	
	
	
	
	
}